/*
 * DAOImpl.java
 * 
 * Versao: 1.0.0
 *
 * Data: 21/01/2011
 *
 * Copyright (C) 2011
 * wsh-systems.
 * Todos os direitos reservados.
 */
package com.wsh.playeronline.persistence.daoImpl;

import java.util.List;

import javax.persistence.EntityManager;

import com.wsh.playeronline.persistence.dao.DAO;

/**
 * Implementacao do DAO.
 * 
 * @author Washington
 * @version 1.0, 21/01/2011
 * @param <T>
 */
public class DAOImpl<T> implements DAO<T> {

	private final Class<T> classe;
	private final EntityManager em;

	/**
	 * Constructor get to EntityManager.
	 * 
	 * @param em EntityManager
	 * @param classe the object
	 */
	public DAOImpl(final EntityManager em, final Class<T> classe) {
		this.em = em;
		this.classe = classe;
	}

	/**
	 * {@inheritDoc}
	 */
	public void adiciona(T t) {
		this.em.persist(t);		
	}

	/**
	 * {@inheritDoc}
	 */
	public void altera(T t) {
		this.em.merge(t);		
	}

	/**
	 * {@inheritDoc}
	 */
	public T busca(Long codigo) {
		return this.em.find(classe, codigo);
	}

	/**
	 * {@inheritDoc}
	 */
	public void deleta(T t) {
		this.em.remove(t);
	}

	/**
	 * {@inheritDoc}
	 */
	public List<T> lista() {
		
		/*
		 * EntityManager em = new JPAUtil().getEntityManager();
				
		//TODO colocar a consulta que lista os status ativos
		Query query = em.createQuery("from :tabela t where t.status = :status");
		query.setParameter("tabela", t);
		query.setParameter("status", Status.ATIVO);		
		 */
		
		return this.em.createQuery("from " + classe.getName()).getResultList();
	}

	/**
	 * {@inheritDoc}
	 */
	public void remove(T t) {		
		
	}
	
}